Week 06: Embedded Programming

Welcome! In this guide, we'll discuss how to program our component. After reviewing this week's assignment and consulting with some classmates, I made the decision to create a basic code for the PCB (Printed Circuit Board) to play the Happy Birthday song. Let's dive into the steps to achieve this

Grupal Assigment

Embedded Programming Overview

MCUs (Microcontrollers):

ATTiny45/85

Comparison:

Evaluating MCUs based on functionalities, dimensions, and performance.

Arithmetic Operations:

Comparison of arithmetic operations between 8-bit and 32-bit microcontrollers.

Cheat Sheet:
  • Syntax rules, control structures, functions, comments, basic GPIO usage, analog readings, timers, printing, PWM, and interrupts in Python and C++.
Examples:
  • Basic GPIO usage in Python, Arduino (C++), and MicroPython.
  • Analog readings in Python, Arduino (C++), and MicroPython.
  • Timers with blocking and non-blocking delays in Python, Arduino (C++), and MicroPython.
  • PWM usage in Python, Arduino (C++), and MicroPython.
  • Interrupt handling in Python and Arduino (C++).
  • Examples for using neopixels with Arduino, MicroPython, and CircuitPython.

Additionally, there are references to UF files for MicroPython and CircuitPython.


PROGRAMMING THE BOARD

For this practice I installed ARDUINO , MU EDITOR and THONNY


Arduino

Just to prove a difrent languaje I looked for an Arduino code that played the same song, just to compare the function and the sound of both languages. Then I found this site ARDUINO SONGS "


Thonny

After doing a practice with Mu and Arduino i desided that Thonny provided a more simple interface, making it easy for me as a beginner to start coding in Python. Helping me reduce distractions, and allowing me detect easily where to star, how to stop and focus on writing and testing your code.

Overall, if you're new to programming or prefer a straightforward and Python-centric development environment, Thonny can be an excellent choice for writing and testing Python code.

  • Install and use First look for the sofware at the tools and open the main and copy the basic code or start codeing. For the PCB to mach the xiao i needed to install RP2 or the predestinated library
  • Also dont forget to make sure you installed the correct circuit python library once you installed it and this window comes up

    This is how the interface looks like with my first example code

  • Read the program: First we need to conect the board pressing the B bottom and it should look like the first pick then after getting the library download in the Xiao rp 2040 we need to set the port with the software just like I show in the pictures .
  • once you cliked options you need to look for the library "MicroPython (2040)"

    and do the same thing for the port at the bottom of the screen and select the port and microcontroller you are using

  • Play the program: In this part I conect the PCB and it sould not open the library, just select the port as I stated before and press play if you deside to sabe the code I sugest to save it in the computer beacuse each time I connected the board it used to reset the files within the RPI RP2.
  • if this window pops up click this device

    And you can play the code with run and stop, To make sure it is runing that window will show up with the code that you just save.


    FIRST CODE
    1. Understand the notes: First, you need to understand the frequencies associated with each note in the song you want to play. You can find tables or resources online that list the frequencies for each note.
    2. Map notes to frequencies: Once you have the frequencies for each note, you'll need to map them to corresponding frequencies in your code. You can use arrays, dictionaries, or switch statements to do this mapping.
    3. Define timing: Decide on the timing for each note. This includes the duration each note should play for and any rests between notes.
    4. Write the code: Using a programming language suitable for your hardware (such as C for Arduino or Python for Raspberry Pi), write code that iterates through the notes of the song, plays each note's corresponding frequency for the specified duration, and includes any necessary rests.
    5. Testing and adjustments: Test your code on your hardware to ensure it plays the song correctly. You may need to adjust the timing or frequencies to get the desired result.
    PLAY SONG

    Here's a simple example in Python for playing the "Happy Birthday" song using the frecuencies from Piano key frequencies - Wikipedia


    Timing of Playing the Song:

    The timing of playing the "Happy Birthday" song on the Xiao RP2040 will involve determining the duration of each note and any rests between notes. This timing can be achieved by specifying the duration of each note in milliseconds or microseconds, depending on the capabilities of the board and the programming language used.

    For example, a simple timing for playing the "Happy Birthday" song might involve playing each note for a fixed duration (e.g., 0.00001 milliseconds) and adding short rests (e.g., 100 milliseconds) between notes to create the rhythm of the song. However, the timing can be adjusted based on personal preference or to better match the original melody of the song.

    FINAL RESULT

    The final result would be a code written in a suitable programming language (such as C/C++ for the Arduino platform, CircuitPython or in this case MicroPython for the RP2040) that runs on the Xiao RP2040 microcontroller board. The code will play the "Happy Birthday" song by generating specific frequencies for each note and controlling the timing of each note's duration.

    Overall, the purpose was fulfilled by a code that accurately plays the "Happy Birthday" song on the Xiao RP2040, with precise timing for each note to create a recognizable rendition of the melody.



    Reflection Summary

    This week, I focused on programming the board using various tools, including Arduino, Mu Editor, and Thonny. My main goal was to compare different programming environments and ultimately play a song using the Xiao RP2040 board.

    Experimenting with different languages and tools helped me understand the nuances of each platform. I found Thonny particularly useful due to its beginner-friendly interface and ease of use for Python programming.

    Challenges and Solutions

    One challenge was understanding the different environments and how they interact with the Xiao RP2040. For instance, ensuring the correct library installations and configuring the ports correctly in Thonny took some time. To solve these issues, I referred to online documentation and tutorials.

    Another challenge was mapping the song notes to their respective frequencies and ensuring the timing was accurate. I utilized resources such as frequency tables and adjusted the code iteratively to achieve the desired result.

    Future Applications

    The knowledge gained this week is valuable for any future projects involving microcontrollers and programming in different environments. Understanding how to interface with hardware using various programming languages will be crucial for developing more complex projects.

    Additionally, mastering tools like Thonny and Arduino will help in efficiently managing and executing coding tasks, making the development process smoother and more efficient.